package com.zyf.project.capchain.dto;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.zyf.project.capchain.model.User;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @author Zhou Yifan
 */
@Data
@TableName(value = "user")
public class UserDTO implements Serializable {

    /**
     * 用户id，采用雪花算法生成
     */
    @TableId(value = "uid", type = IdType.ASSIGN_ID)
    private Long uid;

    /**
     * 小程序登录的 openid
     */
    private String openid;

    /**
     * 用户昵称
     */
    private String nickname;

    /**
     * 用户真实姓名
     */
    private String realname;

    /**
     * 用户头像
     */
    private String avatar;

    /**
     * 用户的手机号
     */
    private String phoneNumber;

    /**
     * 用户登录密码
     */
    private String password;

    /**
     * 用户性别
     */
    private String gender;

    /**
     * 小程序登录的 session_key
     */
    private String sessionKey;

    /**
     * 用户token
     */
    @TableField(exist = false)
    private String token;

    /**
     * 是否为老用户
     */
    @TableField(exist = false)
    private Boolean isOld;

    /**
     * 创建时间
     */
    @TableField(fill = FieldFill.INSERT)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdTime;

    /**
     * 修改时间
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime modifiedTime;

    public UserDTO() {
        this.createdTime = LocalDateTime.now();
        this.modifiedTime = LocalDateTime.now();
    }

    public UserDTO(String openid, String sessionKey) {
        this.openid = openid;
        this.sessionKey = sessionKey;
        this.createdTime = LocalDateTime.now();
        this.modifiedTime = LocalDateTime.now();
    }

    /**
     * DTO 转 Model
     *
     * @return User
     */
    public User toModel() {
        User user = new User();
        user.setUid(uid);
        user.setOpenid(openid);
        user.setNickname(nickname);
        user.setRealname(realname);
        user.setAvatar(avatar);
        user.setPhoneNumber(phoneNumber);
        user.setPassword(password);
        user.setGender(gender);
        user.setSessionKey(sessionKey);
        user.setToken(token);
        user.setIsOld(isOld);
        user.setCreatedTime(createdTime);
        user.setModifiedTime(modifiedTime);
        return user;
    }

}
